package warmachineroster.presentation.dnd;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.requests.CreationFactory;

import warmachineroster.RosterElement;
import warmachineroster.UnitRosterElement;
import warmachineroster.WarcasterRosterElement;
import warmachineroster.WarjackRosterElement;
import warmachineroster.WarmachinerosterFactory;
import warmachineroster.WarmachinerosterPackage;
import warmachinesystem.BaseUnit;
import warmachinesystem.Solo;
import warmachinesystem.Unit;
import warmachinesystem.UnitAttachment;
import warmachinesystem.Warcaster;
import warmachinesystem.Warjack;

public class WarmachineRosterCreationFactory implements CreationFactory {

	private List<BaseUnit> transfer;
	private List<RosterElement> result;

	@Override
	public Object getNewObject() {
		if (result != null)
			return result;
		result = new ArrayList<RosterElement>();
		WarmachinerosterFactory wmFactory = WarmachinerosterPackage.eINSTANCE
				.getWarmachinerosterFactory();
		for (BaseUnit bu : transfer) {

			if (bu instanceof Unit) {
				UnitRosterElement ure = wmFactory.createUnitRosterElement();
				ure.setUnitType((BaseUnit) bu);
				ure.setModelCount(((Unit)bu).getBaseModelNumber());
				result.add(ure);
			} else if (bu instanceof Warcaster) {
				WarcasterRosterElement wre = wmFactory
						.createWarcasterRosterElement();
				wre.setUnitType(bu);
				result.add(wre);
			} else if (bu instanceof Warjack) {
				WarjackRosterElement wre = wmFactory
						.createWarjackRosterElement();
				wre.setUnitType(bu);
				result.add(wre);
			} else if (bu instanceof Solo) {
				RosterElement sre = wmFactory.createRosterElement();
				sre.setUnitType(bu);
				result.add(sre);
			} else if (bu instanceof UnitAttachment) {
				RosterElement uare = wmFactory.createRosterElement();
				uare.setUnitType(bu);
				result.add(uare);
			} else {
				throw new RuntimeException("Unsupported Unit Type");
			}
 		}
		return result;
	}

	@Override
	public Object getObjectType() {
		return null;
	}

	public void setTransferredObjects(List<BaseUnit> transfer) {
		if (this.transfer != transfer) {
			this.transfer = transfer;
			result = null;
		}
	}

}
